<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Csv_reader {

    var $fields;            /** columns names retrieved after parsing */
    var $separator  	=   ';';    /** separator used to explode each line */
    var $enclosure 	 	=   '"';    /** enclosure used to decorate each field */
    var $max_row_size   =   4096;    /** maximum row size to be used for decoding */



    function get_entity_info($tbl_name){

//     	$created_datetime = 'now()';

    	$entitys = array(
		    		'customer' =>  array(
	    					array(
// 				    		'customer_code'  				=> '',// AUTO_INCREMENT
				    		'customer_group_code' 	 		=> '',
				    		'last_name'  					=> '',
				    		'first_name' 					=> '',
				    		'last_name_kana' 				=> '',
				    		'first_name_kana'				=> '',
				    		'login_id'						=> '',
				    		'email'							=> '',
				    		'password' 						=> '',
				    		'birth_date' 					=> '20001122',
				    		'sex' 							=> '1',
				    		'request_mail_type' 			=> '',
				    		'client_mail_type' 				=> '',
				    		'caution' 						=> '',
				    		'login_datetime' 				=> '',
				    		'login_error_count' 			=> '',
				    		'login_locked_flg' 				=> '',
				    		'customer_status' 				=> '',
				    		'customer_attribute_reply_date' => '',
				    		'latest_point_acquired_date'	=> '',
				    		'rest_point' 					=> '',
				    		'temporary_point' 				=> '',
				    		'withdrawal_request_date' 		=> '',
				    		'withdrawal_date' 				=> '',
				    		'orm_rowid' 					=> ''
// 	    							,
// 				    		'created_user' 					=> nullvalue,
// 				    		'created_datetime' 				=> created_datetime,
// 				    		'updated_user' 					=> nullvalue,
// 				    		'updated_datetime' 				=> updated_datetime
	    					),
    						array('customer' => 'customer_code')//PRIMARY KEY
    					),
		    		'commodity_header' => array(
		    				array(
			    			'shop_code'						=> '',
// 							 'commodity_code'		=> '',//AUTO_INCREMENT
							 'commodity_name'				=> '',
							'commodity_type'				=> '',
							 'represent_sku_code'			=> '',
							 'represent_sku_unit_price'		=> '',
							'represent_sku_discount_price'	=> '',
							 'stock_status_no'				=> '',
							 'stock_management_type'		=> '',
							'volume_discount_no'			=> '',
							 'commodity_tax_type'			=> '',
							 'short_description'			=> '',
							 'commodity_search_words'		=> '',
							'shadow_search_words'			=> '',
							 'sale_start_datetime'			=> '',
							 'sale_end_datetime'			=> '',
							 'change_unit_price_datetime'	=> '',
							'discount_price_start_datetime'	=> '',
							 'discount_price_end_datetime'	=> '',
							 'reservation_start_datetime'	=> '',
							'reservation_end_datetime'		=> '',
							 'delivery_type_no'				=> '',
							 'manufacturer_model_no'		=> '',
							 'link_url'						=> '',
							 'recommend_commodity_rank'		=> '',
							'commodity_popular_rank'		=> '',
							 'commodity_standard1_code'		=> '',
							 'commodity_standard2_code'		=> '',
							 'commodity_point_rate'			=> '',
							'commodity_point_start_datetime'=> '',
							 'commodity_point_end_datetime'	=> '',
							 'sale_flg'						=> '',
							 'display_client_type'			=> '',
							 'arrival_goods_flg'			=> '',
							 'oneshot_order_limit'			=> '',
							 'standard_image_type'			=> ''
// 		    						,
// 			    			 'created_user'					=> '',
// 							 'created_datetime'				=> '',
// 							 'updated_user'					=> '',
// 							 'updated_datetime'				=> ''
			    			),
    						array('commodity_header' => array('commodity_code','shop_code'))//PRIMARY KEY
    					),
		    		'commodity_detail' => array(
		    				array(
			    			'shop_code'						=> '',
							 'commodity_code'				=> '',
							 'sku_code'						=> '',
							 'unit_price'					=> '',
							 'discount_price'				=> '',
							 'reservation_price'			=> '',
							'change_unit_price'				=> '',
							 'jan_code'						=> '',
							 'display_order'				=> '',
							 'standard_detail1_code'		=> '',
							 'standard_detail2_code'		=> ''

// 			    			'created_user'					=> '',
// 							'created_datetime'				=> '',
// 							 'updated_user'					=> '',
// 							 'updated_datetime'				=> ''
		    				),
	    					array('commodity_header' => array('commodity_code','shop_code'),
    							  'commodity_detail' => array('shop_code','sku_code')
    						)//PRIMARY KEY
	    			),
		    		'commodity_description' => array(
		    				array(
			    			'shop_code'					    => '',
							'commodity_code'				=> '',
							'description_content'			=> ''
// 		    						,

// 			    			 'created_user'					=> '',
// 							 'created_datetime'				=> '',
// 							 'updated_user'					=> '',
// 							 'updated_datetime'				=> ''
			    			),
		    				array('commodity_header'      => array('0','commodity_code','shop_code'),
			    				  'commodity_description' => array('1','shop_code','commodity_code')
			    			)//PRIMARY KEY
			    		)
		    );

    		$rtnEntityInfo = $entitys[$tbl_name];
    		//共通項目追加
//     		$rtnEntityInfo[] = (
//     				'created_user' 					=> $this->nullvalue,
// 				    'created_datetime' 				=> $this->created_datetime,
// 				    'updated_user' 					=> $this->nullvalue,
// 				    'updated_datetime' 				=> $this->updated_datetime);
		    return  $rtnEntityInfo;
    }
    function parse_file($p_Filepath)
    {
        $file           =   fopen($p_Filepath, 'r');
        $this->fields   =   fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
        $keys_values        =   explode(',',$this->fields[0]);

        $content            =   array();
        $keys           =   $this->escape_string($keys_values);

        $i  =   1;
        while(($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false )
        {
            if( $row != null ) { // skip empty lines
                $values         =   explode(',',$row[0]);
                if(count($keys) == count($values)){
                    $arr            =   array();
                    $new_values =   array();
                    $new_values =   $this->escape_string($values);
                    for($j=0;$j<count($keys);$j++){
                        if($keys[$j]    !=  ""){
                            $arr[$keys[$j]] =   $new_values[$j];
                        }
                    }
                    $content[$i]    =   $arr;
                    $i++;
                }
            }
        }
        fclose($file);
        return $content;
    }
    //一括importのため
    function parse_file_for_batch($p_Filepath)
    {
    	$file           =   fopen($p_Filepath, 'r');
    	$this->fields   =   fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);
    	$keys_values        =   explode(',',$this->fields[0]);

    	$content            =   array();
    	$keys           =   $this->escape_string($keys_values);

    	$i  =   1;
    	while(($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false )
    	{
    		if( $row != null ) { // skip empty lines
    			$values         =   explode(',',$row[0]);
    			if(count($keys) == count($values)){
    				$arr            =   array();
    				$new_values =   array();
    				$new_values =   $this->escape_string($values);
    				for($j=0;$j<count($keys);$j++){
    					if($keys[$j]    !=  ""){

    							//$arr[$keys[$j]] =   $new_values[$j];

    							$content[$keys[$j]] = $new_values[$j];
    					}
    				}
    				//$content[$i]    =   $arr;
    				//$content[] =  $arr;

    				$i++;
    			}
    		}
    	}
    	fclose($file);
    	return $content;
    }
    function escape_string($data)
    {
        $result =   array();
        foreach($data as $row){
            $result[]   =   str_replace('"', '',$row);
        }
        return $result;
    }
}
?>